<!doctype html>



  


<html class="theme-next muse use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.1" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="Linux," />








  <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=5.1.1" />






<meta name="description" content="&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;使用ps  aux  查看系统进程时，第六列即 RSS列显示的就是进程使用的物理内存。 &amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;可是把系统所有进程的该列相加时，得到的总和又远远高于系统实际的物理内存？这到底是怎么回事呢？ &amp;#160;&amp;#160;&amp;">
<meta name="keywords" content="Linux">
<meta property="og:type" content="article">
<meta property="og:title" content="PS出的RSS总和大于实际物理内存">
<meta property="og:url" content="https://hcldirgit.github.io/2017/09/03/1. Linux 基础/53. PS出的RSS总和大于实际物理内存/index.html">
<meta property="og:site_name" content="失落的乐章">
<meta property="og:description" content="&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;使用ps  aux  查看系统进程时，第六列即 RSS列显示的就是进程使用的物理内存。 &amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;可是把系统所有进程的该列相加时，得到的总和又远远高于系统实际的物理内存？这到底是怎么回事呢？ &amp;#160;&amp;#160;&amp;">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2017-09-01T08:30:35.317Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="PS出的RSS总和大于实际物理内存">
<meta name="twitter:description" content="&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;使用ps  aux  查看系统进程时，第六列即 RSS列显示的就是进程使用的物理内存。 &amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;可是把系统所有进程的该列相加时，得到的总和又远远高于系统实际的物理内存？这到底是怎么回事呢？ &amp;#160;&amp;#160;&amp;">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Muse',
    sidebar: {"position":"left","display":"post","offset":12,"offset_float":0,"b2t":false,"scrollpercent":false},
    fancybox: true,
    motion: true,
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://hcldirgit.github.io/2017/09/03/1. Linux 基础/53. PS出的RSS总和大于实际物理内存/"/>





  <title>PS出的RSS总和大于实际物理内存 | 失落的乐章</title>
</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  




<script>
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
            (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
          m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
  ga('create', '85*****1', 'auto');
  ga('send', 'pageview');
</script>


  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?87980c**************99ec5e26fb5";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>











  
  
    
  

  <div class="container sidebar-position-left page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">失落的乐章</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">技术面前，永远都是学生。</p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-message">
          <a href="/message" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-external-link"></i> <br />
            
            留言
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
    <link itemprop="mainEntityOfPage" href="https://hcldirgit.github.io/2017/09/03/1. Linux 基础/53. PS出的RSS总和大于实际物理内存/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="失落的乐章">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/0.png">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="失落的乐章">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">PS出的RSS总和大于实际物理内存</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-09-03T01:55:11+08:00">
                2017-09-03
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;使用ps  aux  查看系统进程时，第六列即 RSS列显示的就是进程使用的物理内存。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;可是把系统所有进程的该列相加时，得到的总和又远远高于系统实际的物理内存？这到底是怎么回事呢？</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;看一看linux是如何管理内存的就会知道。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;理解的意思是这样的，linux会在每个进程生成时分配一定量的内存给这个进程，这个只是分配，而体现在ps出来的是VSZ那列，这叫做虚拟内存。但实际上这些进程并没有占用这些内存。不妨，我也借用网上的一个例子来形容一下，就像银行发工资给员工一样，每个员工都有一个自己的银行卡，每月银行都会把固定的钱数打到员工的银行卡里，但是这个过程并不是把实际的钱发到员工手里，只是一串数字而已。实际上，银行并没有那么多钱的。回头再来看看linux给进程分配内存是不是和上面的举的例子很像呢？</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;讲了上面的观点后，依然不能把笔者所设的问题解答，那么继续往下探讨：</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;把系统所有进程的该列相加时，得到的总和又远远高于系统实际的物理内存？这是因为 ps 的结果，RSS 那部分，是包括共享内存的。这里使用 pmap 来看看。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div></pre></td><td class="code"><pre><div class="line">pmap -d 24030</div><div class="line">24030: /usr/<span class="built_in">local</span>/php/bin/php-cgi --fpm --fpm-config /usr/<span class="built_in">local</span>/php/etc/php-fpm.conf</div><div class="line">Address Kbytes Mode Offset Device Mapping</div><div class="line">0000000000400000 6444 r-x-- 0000000000000000 008:00002 php-cgi</div><div class="line">0000000000c4b000 272 rw--- 000000000064b000 008:00002 php-cgi</div><div class="line">0000000000c8f000 52 rw--- 0000000000c8f000 000:00000 [ anon ]</div><div class="line">00000000059dc000 9572 rw--- 00000000059dc000 000:00000 [ anon ]</div><div class="line">0000003519000000 508 r-x-- 0000000000000000 008:00002 libfreetype.so.6.3.10</div><div class="line">000000351907f000 2048 ----- 000000000007f000 008:00002 libfreetype.so.6.3.10</div><div class="line">中间部分省略</div><div class="line">00002b757df75000 4 rw--- 000000000000a000 008:00002 libnss_files-2.5.so</div><div class="line">00002b757df76000 32768 rw<span class="_">-s</span>- 0000000000000000 000:00008 zero (deleted)</div><div class="line">00002b7580685000 4 rw<span class="_">-s</span>- 0000000000000000 000:00008 zero (deleted)</div><div class="line">00007fff2e126000 476 rwx-- 00007fff2e126000 000:00000 [ stack ]</div><div class="line">00007fff2e19d000 8 rw--- 00007fff2e19d000 000:00000 [ anon ]</div><div class="line">ffffffffff600000 8192 ----- 0000000000000000 000:00000 [ anon ]</div><div class="line">mapped: 139548K writeable/private: 12344K shared: 32772K</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;pmap是用来显示内存使用的指令，-d 后面跟的是进程id. 关于pmap的使用以及显示的数据请看<a href="http://www.lishiming.net/thread-977-1-1.html" target="_blank" rel="external">http://www.lishiming.net/thread-977-1-1.html</a> </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;linux 会把一些shared libraries 载入到内存中，在pmap 的输出中，这些shared      libraries 的名字通常是 lib*.so ,如 libX11.so.6.2.0 。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这个 libX11.so.6.2.0 会被很多process load 到自己的运行环境中，同时，ps 输出的RSS 结果中，每个process 都包含了这个libX11.so.6.2.0 ，而事实上它只被load 了一次，如果单纯把ps 的结果相加，这样就重复计算了。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;看pmap输出的结果，其实php-cgi 单纯进程所占的内存是这个writeable/private: 12344K</p>

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/Linux/" rel="tag"># Linux</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2017/09/03/1. Linux 基础/64. 使用xinetd管理网络应用服务/" rel="next" title="使用xinetd管理网络应用服务">
                <i class="fa fa-chevron-left"></i> 使用xinetd管理网络应用服务
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2017/09/03/1. Linux 基础/68. rsync错误二则、排错过程及解决办法/" rel="prev" title="rsync错误二则、排错过程及解决办法">
                rsync错误二则、排错过程及解决办法 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          
  <div class="comments" id="comments">
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      

      <section class="site-overview sidebar-panel sidebar-panel-active">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="/images/0.png"
               alt="失落的乐章" />
          <p class="site-author-name" itemprop="name">失落的乐章</p>
           
              <p class="site-description motion-element" itemprop="description">失落的乐章的Blog</p>
          
        </div>
        <nav class="site-state motion-element">

          
            <div class="site-state-item site-state-posts">
              <a href="/">
                <span class="site-state-item-count">627</span>
                <span class="site-state-item-name">日志</span>
              </a>
            </div>
          

          

          
            
            
            <div class="site-state-item site-state-tags">
              <a href="/tags/index.html">
                <span class="site-state-item-count">38</span>
                <span class="site-state-item-name">标签</span>
              </a>
            </div>
          

        </nav>

        

        <div class="links-of-author motion-element">
          
            
              <span class="links-of-author-item">
                <a href="https://github.com/hcldirgit" target="_blank" title="GitHub">
                  
                    <i class="fa fa-fw fa-github"></i>
                  
                  GitHub
                </a>
              </span>
            
          
        </div>

        
        

        
        

        


      </section>

      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy; 
  <span itemprop="copyrightYear">2017</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">失落的乐章</span>
</div>


<div class="powered-by">
  由 <a class="theme-link" href="https://hexo.io">Hexo</a> 强力驱动
</div>

<div class="theme-info">
  主题 -
  <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next">
    NexT.Muse
  </a>
</div>


        

        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.1"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.1"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.1"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.1"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.1"></script>



  


  




	





  





  





  






  





  

  

  

  

  

  

</body>
</html>
